import React, {Component} from "react"
import {View, Text, StyleSheet, BackHandler} from "react-native";
import {connect} from "react-redux";
import CustomTabBar from "../navigation/tabBarNavigator"
import {NavigationActions} from "react-navigation";
interface IProps{

}
class Home extends Component<IProps>{
    public props:any
    constructor(props: IProps){
        super(props);
        this.props = props;

    }
    /**
     * @Author:Training
     * @Desc:对于Android物理返回键的适配支持
     * @Params:
     */
    componentDidMount() {
        BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
    }

    componentWillUnmount() {
        BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
    }
    /** * 处理 Android 中的物理返回键 * https://reactnavigation.org/docs/en/redux-integration.html#handling-the-hardware-back-button-in-android * @returns {boolean} */
    onBackPress = () => {
        const {dispatch, nav} = this.props;
        //if (nav.index === 0) {
        if (nav.routes[1].index === 0) {//如果RootNavigator中的MainNavigator的index为0，则不处理返回事件
            return false;
        }
        dispatch(NavigationActions.back());
        return true;
    };
    render():any{
        return (
            <CustomTabBar />
    );
    }
}

const styles = StyleSheet.create({

})
const mapStateToProps = (state:any) => {
    return ({
        theme:state.theme.theme,
        nav:state.nav
});
}
const mapDispatchToProps = (dispatch:any) => ({

});
export default connect(mapStateToProps,mapDispatchToProps)(Home)
